package com.ai.service;

import com.ai.bo.JobBo;
import com.ai.mapper.JobMapper;
import com.ai.service.impl.JobServiceImpl;
import com.ai.vo.JobNameVO;
import com.ai.vo.JobVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wst.ai.pojo.job.Job;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

@Service
public class JobService implements JobServiceImpl {
    @Autowired
    private JobMapper jobMapper;
    /**
     * 根据职位编号查询职位名称
     * @param id
     * @return
     */
    @Override
    public String getNameById(String id) {
        Job job = jobMapper.selectById(id);
        return job.getJobName();
    }

    @Override
    public Job getJobById(String id) {
        Job job = jobMapper.selectById(id);
        return job;
    }

    /**
     * 获取职位列表
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<JobVO> getJobList(int page,int size) {
        PageHelper.startPage(page,  size);
        List<JobVO> jobList = jobMapper.getJobList();
        PageInfo<JobVO> jobVOPageInfo = new PageInfo<>(jobList);
        return jobVOPageInfo;
    }

    @Override
    public void newJob(JobBo jobBo) {
        Job job = new Job();
        BeanUtils.copyProperties(jobBo,job);
        if (StringUtils.isBlank(job.getId())){
            job.setCreateTime(LocalDateTime.now());
            job.setUpdatedTime(LocalDateTime.now());
            jobMapper.insert(job);
        }else {
            job.setUpdatedTime(LocalDateTime.now());
            jobMapper.updateById(job);
        }
    }

    @Override
    public void deleteJob(String id) {
        jobMapper.deleteById(id);
    }

    @Override
    public List<JobNameVO> getNameList() {
        List<Job> jobList = jobMapper.selectList(null);
        List<JobNameVO> jobNameList = new LinkedList<>();
        for (Job job:jobList) {
            JobNameVO jobNameVO = new JobNameVO();
            BeanUtils.copyProperties(job,jobNameVO);
            jobNameVO.setJobId(job.getId());
            jobNameList.add(jobNameVO);
        }
        return jobNameList;
    }
}
